#include "wall.h"

Wall::Wall(float X, float Y, float Z, const btVector3& position, btDynamicsWorld* m_dynamicsWorld, BeColor color)
  : Entity(WALL, color)
{
  commonInit(X,Y,Z,position,m_dynamicsWorld);
}

Wall::Wall(float X, float Y, float Z, const btVector3& position, btDynamicsWorld* m_dynamicsWorld)
{
  type=WALL;
  colors.push_back(BeColor(0.0f,0.0f,0.5f,0.0f));
  commonInit(X,Y,Z,position,m_dynamicsWorld);
}



void Wall::commonInit(float X, float Y, float Z, const btVector3& position, btDynamicsWorld* m_dynamicsWorld)
{
	m_ownerWorld = m_dynamicsWorld;

	halfX		= X/2;
	halfY		= Y/2;
	halfZ		= Z/2;

	groundShape = new btBoxShape( btVector3( halfX ,halfY, halfZ ) );
	
	groundTransform.setIdentity();
	groundTransform.setOrigin( position );

	fixedGround = new btCollisionObject();
	fixedGround->setUserPointer(dynamic_cast<Entity*>(this));
	fixedGround->setCollisionShape(groundShape);
	fixedGround->setWorldTransform(groundTransform);
	m_ownerWorld->addCollisionObject(fixedGround);

	fixedGround->getWorldTransform().getOpenGLMatrix(nposition);
}
	
void Wall::draw()
{
  draw(0);
}

void Wall::draw(int colorsel)
{
  BeColor c = getColor(colorsel);
	glColor4f( c.r, c.g, c.b, c.a );
	glPushMatrix(); 
	Displaylists::Instance()->glMultiMatrix(nposition);
  glScaled(halfX, halfY, halfZ);
	Displaylists::Instance()->call(1);
	glPopMatrix(); 
}

Wall::~Wall()
{
	m_ownerWorld->removeCollisionObject(fixedGround);
	delete groundShape;
	delete fixedGround;
}


